package com.zx.crowdfunding.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zx.crowdfunding.entity.vo.DetailProjectVO;
import com.zx.crowdfunding.entity.vo.PortalTypeVO;
import com.zx.crowdfunding.entity.vo.ProjectVO;
import com.zx.crowdfunding.service.api.ProjectService;
import com.zx.crowdfunding.util.ResultEntity;

/**
 * 项目 - Handler - 远程接口实现
 * @author 郑雪
 * @date 2022-01-01
 */
@RestController
public class ProjectProviderHandler {
	
	/**
	 * 项目Service接口
	 */
	@Autowired
	private ProjectService projectService;
	
	/**
	 * 更新项目进度
	 * <p>修改已筹集到的金额、支持人数、百分比完成度</p>
	 * @param projectId 项目id
	 * @return ResultEntity对象
	 */
	@RequestMapping("/edit/project/progress/remote")
	public ResultEntity<String> editProjectProgressRemote(@RequestParam("projectId") Integer projectId){
		try {
			// 调用本地Service执行更新
			projectService.editProjectProgress(projectId);
			
			// 返回结果
			return ResultEntity.successWithoutData();
								
		} catch (Exception e) {
			e.printStackTrace();
			return ResultEntity.failed(e.getMessage());
		}
	}
	
	
	/**
	 * 获取项目详情
	 * @param projectId 项目id
	 * @return 携带DetailProjectVO对象的ResultEntity对象
	 */
	@RequestMapping("/get/project/detail/remote/{projectId}")
	public ResultEntity<DetailProjectVO> getProjectDetailRemote(@PathVariable("projectId") Integer projectId){
		
		try {
			// 调用本地Service执行查询
			DetailProjectVO detailProjectVO = projectService.getDetailProjectVO(projectId);
			
			// 返回结果
			return ResultEntity.successWithData(detailProjectVO);
								
		} catch (Exception e) {
			e.printStackTrace();
			return ResultEntity.failed(e.getMessage());
		}
		
	}
	
	/**
	 * 获取门户首页的项目分类列表
	 * @return 携带PortalTypeVO对象集合的ResultEntity对象
	 */
	@RequestMapping("/get/portal/type/project/data/remote")
	public ResultEntity<List<PortalTypeVO>> getPortalTypeProjectDataRemote(){
		
		try {
			// 调用本地Service执行查询
			List<PortalTypeVO> portalTypeVOList = projectService.getPortalTypeVOList();
			
			// 返回结果
			return ResultEntity.successWithData(portalTypeVOList);
			
		} catch (Exception e) {
			e.printStackTrace();
			return ResultEntity.failed(e.getMessage());
		}
		
	}
	
	/**
	 * 添加项目
	 * @param projectVO 项目VO对象
	 * @param memberId 会员id
	 * @return ResultEntity对象
	 */
	@RequestMapping("/save/project/remote")
	public ResultEntity<String> saveProjectVORemote(
			@RequestBody ProjectVO projectVO, 
			@RequestParam("memberId") Integer memberId){
		
		try {
			// 调用本地Service执行保存
			projectService.saveProject(projectVO, memberId);
			
			return ResultEntity.successWithoutData();
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
		
	}
	

}
